/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.entity;

import org.ipti.br.tag.entity.enums.Reference;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.ipti.br.tag.util.CalendarUtil;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Entity
public class SimpleFrequency extends AbstractEntity{
    
    @Temporal(TemporalType.DATE)
    @Column(name="frequency_date")
    private Calendar frequencyDate;

    @ManyToOne
    @JoinColumn(name = "actor_id")
    private Actor actor;
    
    @Enumerated(EnumType.ORDINAL)
    private Reference reference;

    @Column(name="reference_id")
    private Long referenceID;

    @Column(length=100)
    private String reason;
    
    @Temporal(TemporalType.DATE)
    @Column(name="replacement_date")
    private Calendar replacementDate;

    public SimpleFrequency() {
    }

    @Override
    public void generateHash(){
        if(getId() == null){
            String hash = ""
                    + ((this.getActor() != null) ? this.getActor().getId() : null)
                    + this.getReference() + this.getReferenceID()
                    + ((this.getFrequencyDate() != null) ? CalendarUtil.getFormatedDate(this.getFrequencyDate()) : null);
            this.setId(Long.valueOf(EncryptionUtil.encrypt(hash).hashCode()));
        }
    }
    
    public SimpleFrequency(Calendar frequencyDate, Actor actor, String reason){
        this.frequencyDate = frequencyDate;
        this.actor = actor;
        this.reason = reason;
    }

    public SimpleFrequency(Calendar frequencyDate, Actor actor, UnitySchedule unitySchedule, String reason){
        this.frequencyDate = frequencyDate;
        this.actor = actor;
        this.referenceID = unitySchedule.getId();
        this.reference = Reference.UNITY;
        this.reason = reason;
    }
    
    public Actor getActor() {
        return actor;
    }

    public void setActor(Actor actor) {
        this.actor = actor;
    }

    public Calendar getFrequencyDate() {
        return frequencyDate;
    }

    public void setFrequencyDate(Calendar frequencyDate) {
        this.frequencyDate = frequencyDate;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public Reference getReference() {
        return reference;
    }

    public void setReference(Reference reference) {
        this.reference = reference;
    }

    public Long getReferenceID() {
        return referenceID;
    }

    public void setReferenceID(Long referenceID) {
        this.referenceID = referenceID;
    }

    public Calendar getReplacementDate() {
        return replacementDate;
    }

    public void setReplacementDate(Calendar replacementDate) {
        this.replacementDate = replacementDate;
    }
}
